package com.example.demo.utils;

import com.example.demo.vo.RTDData;
import com.example.demo.vo.RTDataSetNew;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取类似	effv:[{//有效值
 status:number,//状态：报警预警或者健康
 val:number //具体数值
 },曲线的工具类
 * Created by rex on 2017/6/30.
 */
public class StateValueUtil {
    public static List getThreeValue(String[] kks, long[] timeArray){
        List lists = new ArrayList<>();
        String strResult = KDMOperator.getDataFromKDM(kks,timeArray[0],timeArray[1],2);
        RTDData rtdData = null;
        try {
            rtdData = JsonOperator.getRTDateByJsonString(strResult);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (rtdData.getRTDataSets().length!=3){
            return null;
        }
        if(rtdData.getRTDataSets()[0].getRTDataValues().length==0){
            return null;
        }
        double[] firstValue = new double[rtdData.getRTDataSets()[0].getRTDataValues().length];
        double[] secondValue = new double[rtdData.getRTDataSets()[1].getRTDataValues().length];
        double[] thirdValue = new double[rtdData.getRTDataSets()[2].getRTDataValues().length];
        long[] firstTimes = new long[rtdData.getRTDataSets()[0].getRTDataValues().length];
        long[] secondTimes = new long[rtdData.getRTDataSets()[1].getRTDataValues().length];
        long[] thirdTimes = new long[rtdData.getRTDataSets()[2].getRTDataValues().length];
        if (firstTimes.length != secondTimes.length &&firstTimes.length!=thirdTimes.length){
            return null;
        }
        if (rtdData.getRTDataSets()[0]!=null && rtdData.getRTDataSets()[0].getRTDataValues().length!=0 && rtdData.getRTDataSets()[1]!=null && rtdData.getRTDataSets()[1].getRTDataValues().length!=0&& rtdData.getRTDataSets()[2]!=null&&
                rtdData.getRTDataSets()[2].getRTDataValues().length!=0) {
            for (int i= 0;i<kks.length;i++){
                for (int j= i; j<rtdData.getRTDataSets().length; j++){
                    if (kks[i].equals(rtdData.getRTDataSets()[j].getKksCode())){
                        if (i==j){
                            continue;
                        }
                        RTDataSetNew temp = rtdData.getRTDataSets()[j];
                        rtdData.getRTDataSets()[j] = rtdData.getRTDataSets()[i];
                        rtdData.getRTDataSets()[i] = temp;
                    }
                    continue;
                }
            }
            //获得所有值，其实是有问题的，应该是分开取值的（碰到错再改）
            for (int k = 0; k < rtdData.getRTDataSets()[0].getRTDataValues().length; k++) {
                firstValue[k] = rtdData.getRTDataSets()[0].getRTDataValues()[k].getValue();
                secondValue[k] = rtdData.getRTDataSets()[1].getRTDataValues()[k].getValue();
                thirdValue[k] = rtdData.getRTDataSets()[2].getRTDataValues()[k].getValue();
                firstTimes[k] = rtdData.getRTDataSets()[0].getRTDataValues()[k].getTime();
                secondTimes[k] = rtdData.getRTDataSets()[1].getRTDataValues()[k].getTime();
                thirdTimes[k] = rtdData.getRTDataSets()[2].getRTDataValues()[k].getTime();
            }

        } else {
            return null;
        }
        /**这里应该用等待策略保证三个值是对齐的**/


//       时间本来就是有序的！！尴尬
//
//        for (int m = 0; m<firstTimes.length-1; m++) {
//            for (int n = m + 1; n < firstTimes.length - m - 1; n++) {
//                if (firstTimes[m] > firstTimes[n]) {
//                    long temp1 = firstTimes[n];
//                    firstTimes[n] = firstTimes[m];
//                    firstTimes[m] = temp1;
//                    double temp2 = firstValue[n];
//                    firstValue[n] = firstValue[m];
//                    firstValue[m] = temp2;
//                }
//            }
//        }
//        for (int m = 0; m<secondTimes.length-1; m++) {
//            for (int n = m + 1; n < secondTimes.length - m - 1; n++) {
//                if (secondTimes[m] > secondTimes[n]) {
//                    long temp1 = secondTimes[n];
//                    secondTimes[n] = secondTimes[m];
//                    secondTimes[m] = temp1;
//                    double temp2 = secondValue[n];
//                    secondValue[n] = secondValue[m];
//                    secondValue[m] = temp2;
//                }
//            }
//        }
//
//        for (int m = 0; m<thirdTimes.length-1; m++) {
//            for (int n = m + 1; n < thirdTimes.length - m - 1; n++) {
//                if (thirdTimes[m] > thirdTimes[n]) {
//                    long temp1 = thirdTimes[n];
//                    thirdTimes[n] = thirdTimes[m];
//                    thirdTimes[m] = temp1;
//                    double temp2 = thirdValue[n];
//                    thirdValue[n] = thirdValue[m];
//                    thirdValue[m] = temp2;
//                }
//            }
//        }
        //得到报警状态与检测状态的最大值


        double[] stateList = new double[firstValue.length];
        for (int i = 0; i<firstValue.length; i++) {
            if(firstValue[i]>=secondValue[i]){
                stateList[i] = firstValue[i];
            } else{
                stateList[i] = secondValue[i];
            }
        }
        lists.add(stateList);
        lists.add(thirdValue);
        lists.add(thirdTimes);
        return lists;

    }

}
